﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using He.Common;
using He.Web.UI;

namespace He.Web.manage.tools
{
    public partial class copy_channel : He.Web.UI.Manage
    {
        public int channel_id;                                //栏目ID
        protected void Page_Load(object sender, EventArgs e)
        {
            channel_id = GetInt("channel_id", 0);

            if (!Page.IsPostBack)
            {
                chkLoginLevel("data_copy");
                ddlChannelBind();
                RptBind();
            }
        }

        #region 栏目绑定
        private void ddlChannelBind()
        {
            List<Model.Lang> lst = new He.BLL.config().LoadConfig<List<Model.Lang>>(LangXmlPath);

            string lang_list = "";
            foreach (Model.Lang item in lst)
            {
                if (item.IsShow && item.Value != "cn")
                {
                    lang_list = lang_list + "," + item.Value + ",";
                }
            }


            BLL.channel sbll = new BLL.channel();
            ddlFromChannelId.Items.Add(new ListItem("请选择栏目", ""));
            ddlToChannelId.Items.Add(new ListItem("请选择栏目", ""));
            //绑定中文类别
            DataTable he_sys = sbll.GetList("is_lock=0 and show_category=1").Tables[0]; 
            foreach (DataRow dr_sys in he_sys.Rows)
            {
                if (dr_sys["lang"].ToString() == "cn")
                {
                    ddlFromChannelId.Items.Add(new ListItem("【" + LangHelper.GetLangVer(dr_sys["lang"]) + "】" + dr_sys["manage_title"].ToString(), dr_sys["id"].ToString()));
                }

                if (lang_list.Contains("," + dr_sys["lang"].ToString() + ","))
                {
                    ddlToChannelId.Items.Add(new ListItem("【" + LangHelper.GetLangVer(dr_sys["lang"]) + "】" + dr_sys["manage_title"].ToString(), dr_sys["id"].ToString()));
                }
            }
        }
        #endregion

        #region 数据绑定
        private void RptBind()
        {
            if (this.channel_id > 0)
            {
                this.ddlFromChannelId.SelectedValue = this.channel_id.ToString();
            }
            rptList.DataSource = new BLL.category().GetList(0, channel_id, "id asc");
            rptList.DataBind();
        }
        #endregion

        #region 下级分类绑定
        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Literal LitFirst = (Literal)e.Item.FindControl("LitFirst");
                HiddenField txtlayer = (HiddenField)e.Item.FindControl("txtlayer");
                string LitStyle = "<span style=width:{0}px;text-align:right;display:inline-block;>{1}{2}</span>";
                string LitImg1 = "<img src=../images/folder_open.gif align=absmiddle />";
                string LitImg2 = "<img src=../images/t.gif align=absmiddle />";

                int layer = Convert.ToInt32(txtlayer.Value);
                if (layer == 1)
                {
                    LitFirst.Text = LitImg1;
                }
                else
                {
                    LitFirst.Text = string.Format(LitStyle, layer * 18, LitImg2, LitImg1);
                }
            }
        }
        #endregion

        protected void ddlFromChannelId_SelectedIndexChanged(object sender, EventArgs e)
        {
            int _channel_id;
            if (int.TryParse(this.ddlFromChannelId.SelectedValue.ToString(), out _channel_id))
            {
                Response.Redirect("copy_channel.aspx?channel_id=" + _channel_id);
            }
            else
            {
                Response.Redirect("copy_channel.aspx");
            }
        }
    }
}